from django.db import models
from django.contrib.auth.models import User


class Group(models.Model):
    """小组信息"""
    name = models.CharField(max_length=100, verbose_name='小组名称', db_comment='小组名称')
    description = models.TextField(null=True, blank=True, verbose_name='小组简介', db_comment='小组简介')
    avatar = models.ImageField(upload_to='group_avatars/', null=True, blank=True, verbose_name='小组头像', db_comment='小组头像')
    creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_groups', verbose_name='创建者', db_comment='创建者ID')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')
    is_active = models.BooleanField(default=True, verbose_name='是否活跃', db_comment='是否活跃')

    class Meta:
        verbose_name = '小组'
        verbose_name_plural = '小组'
        db_table = 'zhahuo_group'
        db_table_comment = '小组信息表'

    def __str__(self):
        return self.name


class GroupMember(models.Model):
    """小组成员关联"""
    ROLE_CHOICES = [
        ('leader', '组长'),
        ('member', '成员'),
    ]
    
    group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='members', verbose_name='所属小组', db_comment='所属小组ID')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='group_memberships', verbose_name='成员', db_comment='成员用户ID')
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='member', verbose_name='角色', db_comment='角色:leader组长,member成员')
    joined_at = models.DateTimeField(auto_now_add=True, verbose_name='加入时间', db_comment='加入时间')
    is_active = models.BooleanField(default=True, verbose_name='是否在组', db_comment='是否在组')

    class Meta:
        verbose_name = '小组成员'
        verbose_name_plural = '小组成员'
        db_table = 'zhahuo_group_member'
        db_table_comment = '小组成员关联表'
        unique_together = ['group', 'user']

    def __str__(self):
        return f'{self.user.username} - {self.group.name}'


class Meeting(models.Model):
    """会议信息"""
    STATUS_CHOICES = [
        ('pending', '待开始'),
        ('ongoing', '进行中'),
        ('finished', '已结束'),
    ]
    
    group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='meetings', verbose_name='所属小组', db_comment='所属小组ID')
    title = models.CharField(max_length=200, verbose_name='会议主题', db_comment='会议主题')
    description = models.TextField(null=True, blank=True, verbose_name='会议描述', db_comment='会议描述')
    creator = models.ForeignKey(User, on_delete=models.CASCADE, related_name='created_meetings', verbose_name='创建者', db_comment='创建者ID')
    start_time = models.DateTimeField(verbose_name='开始时间', db_comment='开始时间')
    end_time = models.DateTimeField(null=True, blank=True, verbose_name='结束时间', db_comment='结束时间')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='状态', db_comment='状态:pending待开始,ongoing进行中,finished已结束')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '会议'
        verbose_name_plural = '会议'
        db_table = 'zhahuo_meeting'
        db_table_comment = '会议信息表'
        ordering = ['-start_time']

    def __str__(self):
        return self.title


class MeetingParticipant(models.Model):
    """会议参与者"""
    STATUS_CHOICES = [
        ('invited', '已邀请'),
        ('joined', '已加入'),
        ('left', '已离开'),
    ]
    
    meeting = models.ForeignKey(Meeting, on_delete=models.CASCADE, related_name='participants', verbose_name='会议', db_comment='会议ID')
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='meeting_participations', verbose_name='参与者', db_comment='参与者ID')
    join_time = models.DateTimeField(null=True, blank=True, verbose_name='加入时间', db_comment='加入时间')
    leave_time = models.DateTimeField(null=True, blank=True, verbose_name='离开时间', db_comment='离开时间')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='invited', verbose_name='状态', db_comment='状态:invited已邀请,joined已加入,left已离开')

    class Meta:
        verbose_name = '会议参与者'
        verbose_name_plural = '会议参与者'
        db_table = 'zhahuo_meeting_participant'
        db_table_comment = '会议参与者表'

    def __str__(self):
        return f'{self.user.username} - {self.meeting.title}'


class MeetingMessage(models.Model):
    """会议消息"""
    MESSAGE_TYPE_CHOICES = [
        ('text', '文本'),
        ('image', '图片'),
        ('file', '文件'),
    ]
    
    meeting = models.ForeignKey(Meeting, on_delete=models.CASCADE, related_name='messages', verbose_name='会议', db_comment='会议ID')
    sender = models.ForeignKey(User, on_delete=models.CASCADE, related_name='meeting_messages', verbose_name='发送者', db_comment='发送者ID')
    content = models.TextField(verbose_name='消息内容', db_comment='消息内容')
    message_type = models.CharField(max_length=20, choices=MESSAGE_TYPE_CHOICES, default='text', verbose_name='类型', db_comment='类型:text文本,image图片,file文件')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发送时间', db_comment='发送时间')

    class Meta:
        verbose_name = '会议消息'
        verbose_name_plural = '会议消息'
        db_table = 'zhahuo_meeting_message'
        db_table_comment = '会议消息表'
        ordering = ['created_at']

    def __str__(self):
        return f'{self.sender.username}: {self.content[:20]}'


class MeetingMinutes(models.Model):
    """会议纪要"""
    GENERATED_BY_CHOICES = [
        ('ai', 'AI生成'),
        ('manual', '手动编写'),
    ]
    
    meeting = models.OneToOneField(Meeting, on_delete=models.CASCADE, related_name='minutes', verbose_name='会议', db_comment='会议ID')
    summary = models.TextField(verbose_name='AI生成摘要', db_comment='AI生成摘要')
    content = models.TextField(verbose_name='完整纪要', db_comment='完整纪要')
    generated_by = models.CharField(max_length=20, choices=GENERATED_BY_CHOICES, default='ai', verbose_name='生成方式', db_comment='生成方式:ai为AI生成,manual为手动编写')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='生成时间', db_comment='生成时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间', db_comment='更新时间')

    class Meta:
        verbose_name = '会议纪要'
        verbose_name_plural = '会议纪要'
        db_table = 'zhahuo_meeting_minutes'
        db_table_comment = '会议纪要表'

    def __str__(self):
        return f'{self.meeting.title} - 纪要'
